﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
//using System.Web.Mvc.BindingHelpers;

namespace ContactManager.Models
{
    public class ContactManagerRepository : IContactManagerRepository
    {
        private ContactManagerModelDataContext db = new ContactManagerModelDataContext();


        
        //
        // Query Methods
        public Contact GetContact(System.Guid id)
        {
            //db.DeferredLoadingEnabled = false;
             Contact c = db.Contacts.SingleOrDefault(d => d.Id == id);
           // db = null;
            return c;
        }
        //private Contact GetContact(int id, bool newCMMDC)
        //{
        //    ContactManagerModelDataContext db1 = new ContactManagerModelDataContext();//(ContactManagerModelDataContext) new System.Data.Linq.DataContext(System.Configuration.ConfigurationManager.ConnectionStrings["ContactManagerDBConnectionString"].ConnectionString);
        //    //db1.ObjectTrackingEnabled = false;
        //    return db1.Contacts.SingleOrDefault(d => d.Id == id);
        //}

        public IEnumerable<Contact>ListContacts()
        {
            return db.Contacts;
        }
        public void Add(Contact contact)
        {
            db.Contacts.InsertOnSubmit(contact);
        }
        public Contact Add(System.Guid groupId, Contact contactToCreate)
        {
            contactToCreate.Group = GetGroup(groupId);

            db.Contacts.InsertOnSubmit(contactToCreate);

            return contactToCreate;

        }

        public void EditContact(Contact contactToEdit)
        {
            //Contact originalContact = GetContact(contactToEdit.Id);

            //db.Contacts.Attach(contactToEdit);
            //db.Refresh(System.Data.Linq.RefreshMode.KeepCurrentValues, contactToEdit);
            // our design does not have a FK between Consultant and address 
            // as address table is used hold data from many other tables 
            // like Consultant. 
            //db.Contacts.Attach.t_addresses.Attach(pobjAddress); 
            //db.Refresh(System.Data.Linq.RefreshMode.KeepCurrentValues, pobjAddress);
            // db.t_bank_records.Attach(pobjBankRecord); db.Refresh(System.Data.Linq.RefreshMode.KeepCurrentValues, pobjBankRecord); 
            // db.t_contacts.Attach(pobjContactDetails); db.Refresh(System.Data.Linq.RefreshMode.KeepCurrentValues, pobjContactDetails); 
            // db.SubmitChanges();

            //originalContact.Group = GetGroup(groupID);
            //db.SubmitChanges();
            //return contactToEdit;
            try
            {

             
                //tt_customer Tcust = context.tt_customers.Single(c => c.Pk == customer.Pk);

                ContactManagerModelDataContext db1 = new ContactManagerModelDataContext();//(ContactManagerModelDataContext) new System.Data.Linq.DataContext(System.Configuration.ConfigurationManager.ConnectionStrings["ContactManagerDBConnectionString"].ConnectionString);
               //db1.ObjectTrackingEnabled = false;
                //db1.ObjectTrackingEnabled = false;
               // Contact dbContact = db.Contacts.SingleOrDefault(d => d.Id == contactToEdit.Id);

             //Contact dbContact = GetContact(contactToEdit.Id,true);

                
                db1.Contacts.Attach(contactToEdit,true);
                //context.tt_customers.Attach(customer, Tcust); 
                
                
                //context.SubmitChanges();  
                
                //db.Refresh(System.Data.Linq.RefreshMode.KeepCurrentValues, contactToEdit);
                db1.SubmitChanges();
                

            }
            catch (Exception ex) 
            {
                Exception exep = ex;
                //return contactToEdit;
            }
            
        }
        public Contact EditContact(int groupID, Contact contactToEdit)
        {
            //Contact originalContact = GetContact(contactToEdit.Id);

            try
            {
                Contact originalContact = GetContact(contactToEdit.Id);
                //tt_customer Tcust = context.tt_customers.Single(c => c.Pk == customer.Pk);
                
                db.Contacts.Attach(contactToEdit, originalContact);
                //context.tt_customers.Attach(customer, Tcust); 
                
                
                //context.SubmitChanges();  
                
                //db.Refresh(System.Data.Linq.RefreshMode.KeepCurrentValues, contactToEdit);
                db.SubmitChanges();
            }
            catch (Exception ex) 
            {
                Exception exep = ex;
                return contactToEdit;
            }
                
                
                return contactToEdit;
        }
        public Group EditGroup(Group groupToEdit)
        {
            Group originalGroup = GetGroup(groupToEdit.Id);
            originalGroup.Name = groupToEdit.Name;
            db.SubmitChanges();
            return originalGroup;
        }
        public IQueryable<Contact> FindAllContacts()
        {
            return db.Contacts;
        }

        public IQueryable<Contact> FindTodayContacts()
        {
            return from contact in db.Contacts
                   where contact.DateEntered > DateTime.Now.AddDays(-1)
                   orderby contact.DateEntered
                   select contact;
        }
        
        public void Delete(Contact contact)
        {
            //db.RSVPs.DeleteAllOnSubmit(dinner.RSVPs);
            db.Contacts.DeleteOnSubmit(contact);
            //db.SubmitChanges();
        }

       
        public void Save()
        {
            db.SubmitChanges();
        }

        //GROUP
        public Group CreateGroup(Group groupToCreate)
        {
            //db.AddToGroupSet(groupToCreate);
            db.Groups.InsertOnSubmit(groupToCreate);
            db.SubmitChanges();
            return groupToCreate;
        }

        // Group Methods

        public IEnumerable<Group> ListGroups()
        {
            return db.Groups.ToList();
        }
        public IQueryable<Group> FindAllGroups()
        {
            return db.Groups;
        }

        public Group GetFirstGroup()
        {
            return db.Groups.FirstOrDefault();
        }

        public Group GetGroup(System.Guid id)
        {
            return (from g in db.Groups
                    where g.Id == id
                    select g).FirstOrDefault();
        }

        public void DeleteGroup(Group groupToDelete)
        {
            var originalGroup = GetGroup(groupToDelete.Id);
            db.Groups.DeleteOnSubmit(originalGroup);
            db.SubmitChanges();

        }
        
 
    }
}
